/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.order', [
    "ngResource",
    "ngStorage",
])
    // 订单服务
    .service('OrderService', ['$resource','$localStorage','AuthService',function($resource,$localStorage,AuthService){
        var order_res = $resource(mallorder+'orders/:id');
        var order_users = $resource(mallorder+'/orders/bybuyid/:id');
        var order_paystatus = $resource(mallorder+'/orders/:id/paystatus');
        var order_res1 = $resource(mallorder + 'orderlist/bybuyid/:buyid/page');
        var order_status = $resource(mallorder+'/orders/:id/status');
        var update = $resource(mallorder+'/orders/:orderid')
        var $storage = $localStorage.$default({
            cmtorder: '',
        });
        var userid = AuthService.getUserid();
        // 提交订单请求
        this.createOrder = function(order, success_func, error_func){
            var orderinfo = order_res.save({}, order, success_func, error_func);
            return orderinfo;
        }

        //根据订单完成状态和支付状态查询订单
        this.selectOrders = function (status) {
            return order_res1.query({
                buyid: userid,
                status: status.status,
                paystate: status.paystate,
                num: status.num,
                n: status.n
            });
        }
        // 查询订单详情
        this.getOrderById = function(orderid){
            var orderinfo = order_res.get({id:orderid});
            return orderinfo;
        }

        // 获取用户订单列表
        this.getUserOrderList = function(userid){
            return order_users.query({id:userid});
        }

        // 修改订单支付状态
        this.changeOrderStatus = function(orderid, status) {
            order_paystatus.save({id:orderid, status:status});
        }

        // 修改订单状态
        this.changeOrderStatus = function(orderid, status) {
            order_status.save({id:orderid, status:status});
        }
        //更新订单数据
        this.updateorder = function (order, success_func, error_func) {
            update.save({orderid: order.orderid}, order, success_func, error_func);
        }
        //缓存订单信息
        this.cacheOrder = function (order) {
            $storage.cmtorder = order;
            return $storage.cmtorder;
        }

        this.getCacheOreder = function () {
            return $storage.cmtorder;
        }
        this.delcache = function () {
            $storage.cmtorder = '';
        }


        //缓存订单信息
        this.cacheOrderid = function (orderid) {
            $storage.cmtorderid = orderid;
            return $storage.cmtorderid;
        }

        this.getCacheOrederid = function () {
            return $storage.cmtorderid;
        }
        this.delcacheid = function () {
            $storage.cmtorderid = '';
        }
    }])
;
